

export class Application {
    protected _start: boolean = false;

    protected _requestId: number = -1;

    protected _startTime: number = 0;

    // 上一次执行step的时间
    protected _lastTime: number = 0;

    public start(): void {
        if (!this._start) {
            this._start = true;
            this._requestId = -1;
            this._lastTime = -1;
            this._startTime = -1;

            this._requestId = requestAnimationFrame(this.step);
        }
    }


    public stop(): void {
        if (this._start) {
            cancelAnimationFrame(this._requestId);
            this._requestId = -1;
            this._lastTime = -1;
            this._startTime = -1;
            this._start = false;
        }
    }

    public isRunning(): boolean {
        return this._start;
    }

    protected step(timeStamp: number): void {
        if (this._startTime === -1) this._startTime = timeStamp;
        if (this._lastTime === -1) this._lastTime = timeStamp;

        let elaspsedMsec: number = timeStamp - this._startTime;
        let intervalMsec: number = timeStamp - this._lastTime;

        let intervalSec: number = intervalMsec / 1000;
        this._lastTime = timeStamp;

        this.update(elaspsedMsec, intervalMsec);
        this.render();

        requestAnimationFrame((elaspsedMsec: number): void => {
            this.step(elaspsedMsec);
        })
    }

    protected update(elaspsedMsec: number, intervalMsec: number): void {

    }

    protected render(): void {

    }
}